package com.example.threaddemo;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description
 * @Date 2021/8/8
 * @Created by Jonathan
 */
@Slf4j
public class Print {
    public static void main(String[] args) throws InterruptedException {
        Object lock = new Object();
        AtomicInteger integer = new AtomicInteger(0);
        class Turn implements Runnable {
            @Override
            public void run() {
                synchronized (lock) {
                    while (integer.get() <= 100) {
                        log.info("num:{}", integer.getAndIncrement());
                        try {
                            lock.notify();
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    lock.notify();
                }
            }
        }
        Thread t0 = new Thread(new Turn(), "t0");
        Thread t1 = new Thread(new Turn(), "t1");
        t0.start();
        TimeUnit.MILLISECONDS.sleep(1);
        t1.start();
        System.out.println("hello world");
        System.out.println("hello world");
    }
}
